<?php

/**
 * Sidebar plugin
 * Allow user to add his own links to sidebar
 * @version $Id$
 * @copyright 2010
 */

class Sidebar extends Plugin
{
	public $plugin_name = "Sidebar"; //name of plugin
	public $plugin_short_desc = "Sidebar manager"; //short description of plugin
	public $plugin_long_desc = "Manage sidebar and allows user to add own links to sidebar"; //longer description - what does plugin do
	public $version = "1.0"; //version of plugin
	public $has_sidebar = false; //does this plugin have sidebar -> function generate_sidebar()
	public $has_footer = false; //does this plugin have footer -> function generate_footer()
	public $needs_install = true;

	/**
	 * Constructor
	 */
	function __construct()
	{
	}

	public function generate_sidebar()
	{
		global $theme;

		$query = mysql_query("SELECT * FROM yaps_sidebar ORDER BY position ASC");
		if(mysql_num_rows($query) > 0)
		{
			$out = '<div class="sidebar_plugin"><ul>';
			while($item = mysql_fetch_array($query))
			{
				$out .= '<li>'.stripslashes($item['text']).'</li>';
			}
			$out .= '</ul></div>';

			$theme->assign('sidebar',$out,1);
		}
	}

	private function do_get_stuff()
	{
		global $core,$theme;

		//WE WILL EDIT EXISTING ENTRY
		if($core->get('edit'))
		{
			echo '<h3>Edit sidebar entry</h3>';

			$query = mysql_query("SELECT * FROM yaps_sidebar WHERE id = ".$core->get('edit'));
			if(mysql_num_rows($query) > 0)
			{
				$entry = mysql_fetch_array($query);

				echo '<form action="" method="post">
							<textarea name="sidebar_entry" id="sidebar_entry" cols="50">',stripslashes($entry['text']),'</textarea><br />
							<input type="text" name="position" value="',$entry['position'],'" />
							<input type="submit" value="Edit this sidebar entry" />
							<input type="hidden" name="id" value="',$entry['id'],'" />
							</form>';
			}
			else
			{
				$core->err('Bad ID of sidebar entry! Check your DB table `yaps_sidebar`.');
			}
		}
		elseif($core->get('delete')) //LETS DELETE SOMETHING
		{
			$query = mysql_query("DELETE FROM yaps_sidebar WHERE id = ".$core->get('delete'));
			$core->msg('Sidebar entry with ID '.$core->get('delete').' has been deleted.');
			$theme->js('$(location).attr(\'href\',\'admin-pluginsettings?plugin=sidebar\');');
		}
	}

	private function do_post_stuff()
	{
		global $core,$theme;

		//EDITING EXISTING ENTRY
		if($core->post('id'))
		{
			$query = mysql_query("UPDATE yaps_sidebar SET
			text = '".$core->post('sidebar_entry')."',
			position = ".$core->post('position')."
			WHERE id = ".$core->post('id'));

			if($query) $core->msg('Your sidebar entry ID '.$core->post('id').' has been updated.');
			else $core->err('Oops, can`t edit your sidebar entry ID '.$core->post('id').', please check your DB table `yaps_sidebar`.');
			$theme->js('$(location).attr(\'href\',\'admin-pluginsettings?plugin=sidebar\');');
		}
		elseif($core->post('sidebar_entry')) //LETS ADD SOMETHING
		{
			$query = mysql_query("INSERT INTO yaps_sidebar VALUES(
			'0',
			'".$core->post('sidebar_entry')."',
			'".$core->post('position')."'
			)");

			if($query) $core->msg('Your sidebar entry has been added.');
			else $core->err('Oops, can`t insert your sidebar entry, please check your DB table `yaps_sidebar`.');
			$theme->js('$(location).attr(\'href\',\'admin-pluginsettings?plugin=sidebar\');');
		}
	}

	/**
	 * Sidebar::admin_settings()
	 * @return
	 */
	public function admin_settings()
	{
		global $core,$theme;

		if($core->is_plugin('wysiwyg'))
		{
			$wysiwyg = $core->load_plugin('wysiwyg');
			$wysiwyg->create_wysiwyg('sidebar_entry');
		}

		$theme->include_jquery(false);
		$theme->js('$("#ac").accordion({ autoHeight: false });');

		echo '<div id="ac">';

		$this->do_get_stuff();
		$this->do_post_stuff();

		echo '<h3>Add new sidebar entry</h3>
					<form action="" method="post">
						<textarea name="sidebar_entry" id="sidebar_entry" cols="50"></textarea><br />
						Positin: <input type="text" name="position" size="5" /><br />
						<input type="submit" value="Create new sidebar entry" />
					</form>
					<h3>Sidebar entries</h3>';

		$query = mysql_query("SELECT * FROM yaps_sidebar ORDER BY position ASC");
		if(mysql_num_rows($query) > 0)
		{
			echo '<table class="nice">
						<tr><th>Text</th><th>Positon</th><th>Options</th></tr>';

			while($item = mysql_fetch_array($query))
			{
				echo '<tr>
							<td>',$item['text'],'</td>
							<td>',$item['position'],'</td>
							<td><a href="admin-pluginsettings?plugin=sidebar&amp;edit=',$item['id'],'">',$theme->icon('pencil.png','Edit'),'</a>
									<a href="admin-pluginsettings?plugin=sidebar&amp;delete=',$item['id'],'">',$theme->icon('cancel.png','Delete'),'</a>
							</td>
							</tr>';
			}

			echo '</table>';
		}
		else
		{
			echo 'Nothing in there ;)';
		}

		echo '</div>';
	}

	public function install()
	{
		$create_table = mysql_query("CREATE TABLE IF NOT EXISTS `yaps_sidebar` (
		   `id` int(11) NOT NULL AUTO_INCREMENT,
		   `text` text COLLATE utf8_czech_ci NOT NULL,
		   `position` int(11) NOT NULL,
		   PRIMARY KEY (`id`)
		   ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_czech_ci AUTO_INCREMENT=1");

		$insert_data = mysql_query("INSERT INTO `yaps_sidebar` (`id`, `text`, `position`) VALUES
		   (1, '<h4>About me</h4>Yeah, this is me :)', 1)");

		$insert_data_1 = mysql_query('INSERT INTO yaps_config VALUES(\'plugin_sidebar_installed\',\'true\')');

		if($create_table && $insert_data && $insert_data_1) return true;
		else return false;
	}
}


?>